﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace CMTVision.Widgets.Widgets
{
    //[ViewStateModeById]
    public partial class ContentWidget : CMTVision.Widgets.Impls.Widget
    {
        #region Ctor

        protected void Page_Load(object sender, EventArgs e)
        {
            InitPanel(IsInEditting);
            if (!IsPostBack)
            {
                BindData();
            }
            else
            {
                ContentLiteral.Text = EditContent;
            }
        }

        #endregion

        protected string EditContent
        {
            get
            {
                return (ViewState["EditContent"] ?? String.Empty).ToString();
            }
            set
            {
                ViewState["EditContent"] = value;
            }
        }

        #region Override

        public override void Edit()
        {
            InitPanel(true);
            ContentEditor.RadEditor.Height = (String.IsNullOrEmpty(editorHeightHidden.Value) ? 10 : Convert.ToInt32(editorHeightHidden.Value))-5;
            //BindData();
        }

        public override void ForceRefresh()
        {
            InitPanel(false);
            //BindData();
        }

        public override void Save()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("~/Data/templatedata.xml"));
            string xPath = "/templatedatas/templatedata/columns/column/widgets/widget[@id='" + WidgetId + "']/content";
            XmlNode node = xmlDoc.SelectSingleNode(xPath);
            if (node != null)
            {
                node.InnerXml = String.Empty;
                node.AppendChild(xmlDoc.CreateCDataSection(ContentEditor.RadEditor.Content));
                xmlDoc.Save(Server.MapPath("~/Data/templatedata.xml"));
            }

            InitPanel(false);
            BindData();
            WidgetContainer.ForceRefresh();
        }
        #endregion

        #region Save and BindData methods

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            
        }

        void InitPanel(bool inEditing)
        {
            IsInEditting = inEditing;
            EditPanel.Visible = IsInEditting;
            ShowPanel.Visible = !IsInEditting;
            //ShowPanel.Attributes["class"] = IsInEditting ? "ShowPanel hide" : "ShowPanel";
            //EditPanel.Attributes["class"] = !IsInEditting ? "hide" : "";
        }

        void BindData()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("~/Data/templatedata.xml"));
            string xPath = "/templatedatas/templatedata/columns/column/widgets/widget[@id='" + WidgetId + "']";
            XmlNode node = xmlDoc.SelectSingleNode(xPath);
            XmlNode contentNode = node.SelectSingleNode("content");
            if (contentNode == null)
            {
                ContentLiteral.Text = "No Content";
                ContentEditor.RadEditor.Content = "";
                contentNode = xmlDoc.CreateElement("content");
                contentNode.AppendChild(xmlDoc.CreateCDataSection(""));
                node.AppendChild(contentNode);
                xmlDoc.Save(Server.MapPath("~/Data/templatedata.xml"));
            }
            else
            {
                ContentLiteral.Text = contentNode.InnerText;
                ContentEditor.RadEditor.Content = contentNode.InnerText;
            }
            EditContent = ContentEditor.Content;
        }

        #endregion
         
    }
}